<?php
//current operation
$currentOperation = Blyn::app()->getActiveOperation();
$isShowAddMenu = true;
$wxMenu = new BUIWXMenu();

$operationList = $wxMenu->getOperationList();

foreach ($operationList as $id => $name) {
    if ($name == 'weEvent:followEvent') {
        $wxMenu->owner = new BOperation($id);
        $menuItems = $wxMenu->getMenuItems(array('operationId' => $id));
        if ($menuItems == NULL) {
            //add dummy menus for followEvent
            $wxMenu->addWechatMenuItem('商社导航|酒店', $id);
            $wxMenu->addWechatMenuItem('商社导航|汽车', $id);
            $wxMenu->addWechatMenuItem('商社导航|电器', $id);
            $wxMenu->addWechatMenuItem('商社导航|百货', $id);
            $wxMenu->addWechatMenuItem('商社导航|商品促销', $id);
            $menuItems = $wxMenu->getMenuItems(array('operationId' => $id));
        }
    }
}
$select = Blyn::app()->getActiveOperation()->getId();

if ($currentOperation->getUIManager()->isAjaxLinkClicked('Add Menu')) {
    if (!empty($_POST)) {
        if(isset( $_POST['menuStr']))
        {
            $menuStr = $_POST['menuStr'];
            if(isset( $_POST['OperationId']))
            {
                $opId = $_POST['OperationId'];
                $wxMenu->addWechatMenuItem($menuStr, $opId);
                $menuItems = $wxMenu->getMenuItems(array('operationId' => $opId));
            }
        }
    }
}
?>

<style>
 table { table-layout: fixed; }
 table th, table td { overflow: hidden; }
</style>

<?php echo CHtml::beginForm(); ?>
<table class="table table-hover">
    <caption>Wechat Menus:</caption>
    <thead>
        <tr>
            <th style="width: 20%">Menu Item</th>
            <th style="width: 40%">Set Operation</th>
            <th style="width: 40%">Url</th>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach ($menuItems as $menu => $operationId) {
            $select = $operationId;
            $name = $menu;
            $operation = new BOperation($select);
            $wxMenu->owner = $operation;
            $url = $wxMenu->getLinkUrl($name, NULL, $operation->getUIManager());
            ?>
            <tr>
                <td class="span2"><?php echo $name; ?></td>      
                <td class="span2"><?php echo CHtml::dropDownList('Set operation', $select, $operationList); ?></td> 
                <td class="span2"><?php echo $url; ?></td>    
            </tr>
        <?php } ?>
    </tbody>
</table>
<div></div>
<table class="table table-hover">
    <caption>Add New Menu:</caption>
    <thead>
        <tr>
            <th style="width: 20%">Menu Item</th>
            <th style="width: 40%">Set Operation</th>
            <th style="width: 40%">Application ID</th>
        </tr>
    </thead>
    <tbody>        
            <tr>
                <td class="span1"><?php echo CHtml::textField('menuStr', '', array('id'=>'menuStr', 'style'=>'width:100px;')); ?></td>  
                <td class="span1"><?php echo CHtml::dropDownList('OperationId', $select, $operationList); ?></td> 
                <td class="span1"><?php echo CHtml::textField('appId', '', array('id'=>'appId')); ?></td> 
            </tr>
    </tbody>
</table>
<div class="footer">
    <?php echo $currentOperation->getUIManager()->getAjaxLink('Add Menu', array('htmlOptions' => array('class' => 'btn'))); ?>
</div>
<?php echo CHtml::endForm(); ?>
